<html>
<head><meta charset="utf-8"><title>Given two keys, swap their values · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html">Given two keys, swap their values</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="244274815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244274815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244274815">(Jun 29 2021 at 12:51)</a>:</h4>
<p>Hi, </p>
<p>I was thinking of PRing a HashMap::swap(&amp;mut self, a : &amp;Key, b : &amp;Key) method. </p>
<ul>
<li>
<p>Is this something that could be added to the standard library? </p>
</li>
<li>
<p>If so, is there maybe a reason it has not been added?</p>
</li>
</ul>
<p>Description: Given two keys in the HashMap, swap their values using a ptr::swap, similar to swap for slices.</p>
<p>Thanks</p>



<a name="244276461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244276461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244276461">(Jun 29 2021 at 13:03)</a>:</h4>
<p>What happens if a or b aren't in the map? It panics?</p>
<blockquote>
<p>Is there a reason it has not been added?</p>
</blockquote>
<p>It works the other way: if you have an idea for a new method, you should give an example where it would be useful. The default is not to add things (since otherwise the standard library would grow without bound)</p>



<a name="244277546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244277546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244277546">(Jun 29 2021 at 13:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Given.20two.20keys.2C.20swap.20their.20values/near/244276461">said</a>:</p>
<blockquote>
<p>What happens if a or b aren't in the map? It panics?</p>
<blockquote>
<p>Is there a reason it has not been added?</p>
</blockquote>
<p>It works the other way: if you have an idea for a new method, you should give an example where it would be useful. The default is not to add things (since otherwise the standard library would grow without bound)</p>
</blockquote>
<p>Yep, it would panic if <code>a</code> or <code>b</code> are not present. My use case was that I was working on a DisJointSet data structure and I needed to swap the two values in the hashmap. Since I could not take two mutable references at once from the hashmap, this method would have been helpful to have.</p>
<p>Also very fair on the point about standard library bloat :) Let me know if it seems like it is not needed or if the use case is niche</p>



<a name="244304329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244304329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244304329">(Jun 29 2021 at 15:56)</a>:</h4>
<p>you could start with <code>hashbrown</code> itself -- <code>get_each_mut</code> should make it easy</p>



<a name="244304455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244304455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244304455">(Jun 29 2021 at 15:57)</a>:</h4>
<p>or even just move to that for your own use case</p>



<a name="244316668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244316668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244316668">(Jun 29 2021 at 17:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Given.20two.20keys.2C.20swap.20their.20values/near/244304329">said</a>:</p>
<blockquote>
<p>you could start with <code>hashbrown</code> itself -- <code>get_each_mut</code> should make it easy</p>
</blockquote>
<p>Ah so I'm guessing that it is not something that is warranted. I'd prefer to use the regular HashMap with a pointer swap as it's just a simple std import :D</p>



<a name="244320317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244320317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244320317">(Jun 29 2021 at 17:49)</a>:</h4>
<p>hashbrown is the std hashmap implementation</p>



<a name="244321321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244321321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244321321">(Jun 29 2021 at 17:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="330154">The 8472</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Given.20two.20keys.2C.20swap.20their.20values/near/244320317">said</a>:</p>
<blockquote>
<p>hashbrown is the std hashmap implementation</p>
</blockquote>
<p>Oh I thought I had to pull it in as a separate dep</p>



<a name="244321545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Given%20two%20keys%2C%20swap%20their%20values/near/244321545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Given.20two.20keys.2C.20swap.20their.20values.html#244321545">(Jun 29 2021 at 17:58)</a>:</h4>
<p>well, you do, but it's basically the same code, just exposing things that std doesn't.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>